﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using TuyenSinh.BLL;

namespace TuyenSinh.Admin
{
    public partial class DetailEnrolling : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["Admin"] != null)
                {
                    BSchool bSchool = new BSchool();
                    DataTable dt = new DataTable();
                    dt = bSchool.GetRegion();
                    ddlRegionAdd.DataSource = dt;
                    ddlRegionAdd.DataValueField = "Id";
                    ddlRegionAdd.DataTextField = "Name";
                    ddlRegionAdd.DataBind();

                    ddlRegion.DataSource = dt;
                    ddlRegion.DataValueField = "Id";
                    ddlRegion.DataTextField = "Name";
                    ddlRegion.DataBind();




                    dt = bSchool.GetSchool(string.Empty, 0, 0, 0, 1, 0);
                    ddlSchoolAdd.DataSource = dt;
                    ddlSchoolAdd.DataTextField = "Name";
                    ddlSchoolAdd.DataValueField = "Id";
                    ddlSchoolAdd.DataBind();
                    ddlSchoolAdd.Items.Insert(0, new ListItem("-- Chọn trường --", "0"));


                    ddlSchool.DataSource = dt;
                    ddlSchool.DataTextField = "Name";
                    ddlSchool.DataValueField = "Id";
                    ddlSchool.DataBind();
                    ddlSchool.Items.Insert(0, new ListItem("-- Chọn trường --", "0"));

                    BMajor bMajor = new BMajor();
                    dt = bMajor.GetField();

                    ddlFieldAdd1.DataSource = dt;
                    ddlFieldAdd1.DataTextField = "Name";
                    ddlFieldAdd1.DataValueField = "Id";
                    ddlFieldAdd1.DataBind();
                    ddlFieldAdd1.Items.Insert(0, new ListItem("-- Chọn khối1 --", "0"));

                    ddlFieldAdd2.DataSource = dt;
                    ddlFieldAdd2.DataTextField = "Name";
                    ddlFieldAdd2.DataValueField = "Id";
                    ddlFieldAdd2.DataBind();
                    ddlFieldAdd2.Items.Insert(0, new ListItem("-- Chọn khối2 --", "0"));

                  
                }
                else
                {
                    Response.Redirect("Login.aspx");
                }
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            var iRegionId = Convert.ToInt32(ddlRegion.SelectedValue);
            var iSchoolId = Convert.ToInt32(ddlSchool.SelectedValue);
            var sName = txtNameSerch.Text;
            BDetailEnrolling bDetailEnrolling = new BDetailEnrolling();
            DataTable dt = bDetailEnrolling.GetDetailEnrolling(iSchoolId, 0);

            GrvMajor.DataSource = dt;
            GrvMajor.DataBind();

        }

        protected void GrvMajor_OnRowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataTable dt = new DataTable();

                Label lbDetailEnrollingId = (Label)e.Row.FindControl("lbDetailEnrollingId");
                lbDetailEnrollingId.Text = DataBinder.Eval(e.Row.DataItem, "Id").ToString();

                Label lbMajorName = (Label)e.Row.FindControl("lbMajorName");
                BDetailEnrolling bDetail = new BDetailEnrolling();
                dt = bDetail.GetMajor(Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "MajorId")), string.Empty, 0, 0, 1, 0);
                lbMajorName.Text = dt.Rows[0]["Name"].ToString();

                Label lbYear = (Label)e.Row.FindControl("lbYear");
                lbYear.Text = DataBinder.Eval(e.Row.DataItem, "Year").ToString();

                TextBox txtRegister = (TextBox)e.Row.FindControl("txtRegister");
                txtRegister.Text = DataBinder.Eval(e.Row.DataItem, "QuantityRegister").ToString();

                TextBox txtTarget = (TextBox)e.Row.FindControl("txtTarget");
                txtTarget.Text = DataBinder.Eval(e.Row.DataItem, "Targets").ToString();

                TextBox txtScore = (TextBox)e.Row.FindControl("txtScore");
                txtScore.Text = DataBinder.Eval(e.Row.DataItem, "Score").ToString();

                TextBox txtCompetitor = (TextBox)e.Row.FindControl("txtCompetitor");
                txtCompetitor.Text =String.Format("{0:0.##}",Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "CompetitionNum").ToString()));


                //HyperLink lnkEdit = (HyperLink) e.Row.FindControl("lnkEdit");
                //lnkEdit.Text = "Sửa";
                //string lnk = string.Format("DetailEnrolling.aspx?majorid={0}&schoolid={1}&year={2}",
                //                           DataBinder.Eval(e.Row.DataItem, "MajorId").ToString(),
                //                           DataBinder.Eval(e.Row.DataItem, "SchoolId").ToString(),
                //                           DataBinder.Eval(e.Row.DataItem, "Year").ToString());
                //lnkEdit.NavigateUrl = lnk;

            }
        }

        [WebMethod]
        public static string GetMajor(int iMajorId, string sName, int iSectionId, int iSchoolId, int isActive, int isDelete)
        {
            BDetailEnrolling bDetailEnrolling = new BDetailEnrolling();
            DataTable dt = bDetailEnrolling.GetMajor(iMajorId, sName, iSectionId, iSchoolId, isActive, isDelete);
            MajorList list = new MajorList();
            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    list.Lst = new List<MajorItem>();
                    foreach (DataRow dr in dt.Rows)
                    {

                        MajorItem item = new MajorItem();
                        item.Id = dr["Id"].ToString();
                        item.Name = dr["Name"].ToString();
                        list.Lst.Add(item);
                    }
                }
                else
                {
                    list.Lst = null;
                }
            }
            else
            {
                list.Lst = null;
            }
            return new JavaScriptSerializer().Serialize(list);
        }

        [WebMethod]
        public static string InsertDetailEnrolling(int iSchoolId, int iMajorId, int iField1, int iField2, int iNumRegister, int iTarget, float iScore, int iYear, float fNumCompetitor)
        {
            bool bRes = false;
            BDetailEnrolling bDetailEnrolling = new BDetailEnrolling();
            bRes = bDetailEnrolling.InsertDetailEnrolling(iSchoolId, iMajorId, iField1, iField2, iNumRegister, iTarget, iScore, iYear, fNumCompetitor);


            return bRes ? "1" : "0";
        }


        [WebMethod]
        public static string UpdateDetailRolling(int iId, int iNumRegister, int iTargets, float iScore, float fCompetitor)
        {
            BDetailEnrolling bDetail = new BDetailEnrolling();
            bool bRes = bDetail.UpdateDetailEnrolling(iId, iNumRegister, iTargets, iScore, fCompetitor);
            return bRes ? "1" : "0";
        }
    }

    public class MajorItem
    {
        public string Id { get; set; }
        public string Name { get; set; }
    }

    [Serializable]
    public class MajorList
    {
        public List<MajorItem> Lst { get; set; }
        public int Length { get; set; }
    }
}